package divide;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class Division implements Runnable{
	private long startIndex;
	private InputStream inputStream;
	private RandomAccessFile randomAccessFile;
	private List<Byte> list;
	 
	public Division(String file, String copyFile, long startIndex,long endIndex) throws FileNotFoundException {
		this.inputStream = new FileInputStream(file);
		this.randomAccessFile = new RandomAccessFile(copyFile, "rw");
		this.startIndex = startIndex;
		list = new ArrayList<Byte>();
	}
	
	/**
	 * 线程寻至其所负责的文件部分，把文件内容读取出来写到新文件中
	 */
	public void run() {
		try {
			inputStream.skip(startIndex);
			int m=0;
			while((m=inputStream.read())!=-1){
				list.add((byte)m);
			}
			inputStream.close();
			randomAccessFile.seek(startIndex);
			for(int i=0; i<list.size(); i++){
				randomAccessFile.write(list.get(i).byteValue());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
